
from toee import *
from utilities import *
from InventoryRespawn import *
from council import *
from py00439script_daemon import record_time_stamp, get_v, set_v, npc_set, npc_unset, npc_get, tsc, within_rect_by_corners


switch_flag = obj_f_npc_pad_i_5
#used in shopkeeper burne scripting (i.e. when he joins you and stuff, line 994 e.g.)
# will have to be fixed for combat script compatibility!!!!!!


#obj_f_npc_pad_i_4:
# bit 1 - Burne has called on Rufus to attack you
# HAS TO BE CHANGED TO PAD_I_3!!!!!

# 'BurneStory' - persistent flag related to Burne's having told his tale.
# 'BurneHistory' - Battle of Emridy meadows story and the rest.



def san_dialog( attachee, triggerer ):
	attachee.turn_towards( triggerer )
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
	# Burne is a party member (or charmed)
		triggerer.begin_dialog( attachee, 1300 )
	elif game.leader.reputation_has(30) == 1:
	# You are a convict, broke out of jail
		triggerer.begin_dialog( attachee, 17000 )
	elif (attachee.map == 5001 and get_v(435) == 4):
	# Burne float
		attachee.float_line(7500,triggerer)
	elif (get_v(435) == 5 and game.global_flags[431] == 0 and (get_v(436) == 2 or get_v(436) == 6)):
		# have not yet discussed council events; this section is relates to the result being NEGATIVE
		if (get_v(436) == 2):
			# you bolted to the door when the trap went off
			game.global_flags[431] = 1
			triggerer.begin_dialog( attachee, 8600 )
		elif (get_v(436) == 6):
			# you slit throats; this has not gone unnoticed
			game.global_flags[431] = 1
			triggerer.begin_dialog( attachee, 13500 )
	elif ( (game.party[0].reputation_has( 29 ) == 1 or game.global_flags[442] == 1) and game.global_flags[439] == 0 ): 
		if (game.quests[15].state == qs_accepted or game.quests[15].state == qs_mentioned):
			game.quests[15].state = qs_botched
		if (game.quests[64].state == qs_accepted or game.quests[64].state == qs_mentioned):
			game.quests[64].state = qs_botched
		triggerer.begin_dialog( attachee, 16100 )
		#"what is the meaning of your outrageous behavior?!"
	elif (game.global_flags[839] == 1):
		# You snuck Lareth out of prison; this is prioritized above positive council event results, b/c you've pissed Burne off
		triggerer.begin_dialog(attachee,5200)
	elif ( (get_v(435) == 5) and game.global_flags[431] == 0):
		# have not yet discussed council event results; NEUTRAL/POSITIVE results
		game.global_flags[431] = 1
		if (get_v(436) == 1):
			# trap went off when you were away
			triggerer.begin_dialog( attachee, 7600 )
		elif (get_v(436) == 3):
			# you helped evacuate
			triggerer.begin_dialog( attachee, 9000 )
		elif (get_v(436) == 4):
			# found trap before it went off
			triggerer.begin_dialog( attachee, 9400 )
		elif (get_v(436) == 5):
			# badgers guarded without you
			triggerer.begin_dialog( attachee, 13000 )
		elif (get_v(436) == 7):
			# you used poison; what a tragedy
			triggerer.begin_dialog( attachee, 14000 )
		elif (get_v(436) == 8):
			# you guarded Hall
			triggerer.begin_dialog( attachee, 14500 )
	elif (game.global_flags[835] == 1 and game.global_flags[37] == 0 and game.global_flags[842] == 1 and game.global_flags[839] == 0):
		# Lareth is imprisoned (enables asking Burne about it)
		triggerer.begin_dialog(attachee,5000)
	elif (game.party[0].reputation_has( 28 ) == 1 and triggerer.get_stat_level (stat_gender) == gender_female and triggerer.stat_level_get(stat_intelligence) >= 8): 
		# Dominatrix
		triggerer.begin_dialog( attachee, 2200 )
	elif (game.party[0].reputation_has( 27 ) == 1): 
		# Rabble-rouser rep
		triggerer.begin_dialog( attachee, 11002 )
	elif attachee.has_met(triggerer):
		triggerer.begin_dialog( attachee, 3000 )
	else:
		triggerer.begin_dialog( attachee, 1 )
	return SKIP_DEFAULT

def san_first_heartbeat( attachee, triggerer ):
	if (attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		game.global_vars[730] = 0
	if not (game.global_vars[34] & 1) and Shopkeeper_Is(attachee):
		game.timevent_add(respawn, (attachee), 86400000 ) #86400000ms is 24 hours
		game.global_vars[34] = game.global_vars[34] | 1
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	Switch_NPC( attachee ) ## Added by Cerulean the Blue
	#if (game.global_vars[730] == 0 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
	#	attachee.cast_spell(spell_mage_armor, attachee)
	#	attachee.spells_pending_to_memorized()
	#	game.global_vars[730] = 1
	#	Moved pre-buff to 00334 script file
	return RUN_DEFAULT

def san_new_map( attachee, triggerer ):
	if ( game.global_flags[195] == 1 ):
		game.leader.begin_dialog( attachee, 1850 )
	elif game.global_flags[429] == 0 and game.quests[64].state >= qs_completed:
		game.global_flags[429] = 1
		if game.quests[64].state == qs_botched:
			proactivity(attachee, 3890)
		elif game.quests[64].state == qs_completed:
			if attachee.leader_get() != OBJ_HANDLE_NULL:
				triggerer.follower_remove( attachee )
			initialize_council_events()
			proactivity(attachee, 3980)
	return SKIP_DEFAULT

def san_dying( attachee, triggerer ):
	attachee.scripts[41] = 4 # san_spell_cast
	attachee.float_line(12014,triggerer)
	game.global_flags[336] = 1
	record_time_stamp(422)
	if (attachee.leader_get() == OBJ_HANDLE_NULL):
		game.global_vars[23] = game.global_vars[23] + 1
		if (game.global_vars[23] >= 2):
			game.party[0].reputation_add( 1 )
	else:
		game.global_vars[29] = game.global_vars[29] + 1
	return RUN_DEFAULT
	
################################################################################################
 ## Added by Cerulean the Blue

def get_switch_flag( attachee ): # Reads object field where switch flag is stored
	return attachee.obj_get_int( obj_f_npc_pad_i_5 )

def set_switch_flag( attachee, value ): # Sets switch flag in object field where it is stored.  Set to 1 in dialog if NPC is joining party.  Set to 0 in Switch_NPC after NPC has been added to party.
	attachee.obj_set_int( obj_f_npc_pad_i_5, value )
	return attachee.obj_get_int( obj_f_npc_pad_i_5 )

def Switch_NPC( attachee ):
	# Checks if script is being run by Advenrurer and if he has a leader
	if ( not ( Shopkeeper_Is( attachee ) ) and attachee.leader_get() == OBJ_HANDLE_NULL ):	
		# get handle on Shopkeeper
		Shopkeeper = Find_NPC( attachee, 'Shopkeeper' )		
		# If Shopkeeper is "on"  and switch is needed perform "on/off" switch to both and add Adventurer to party 
		if ( ( Shopkeeper != OBJ_HANDLE_NULL ) and ( get_switch_flag( Shopkeeper ) ) ):
			attachee.object_flag_unset(OF_OFF)
			Shopkeeper.object_flag_set(OF_OFF)
			game.party[0].follower_add( attachee )
			set_switch_flag( Shopkeeper, 0 )
		# Add Adventurer to party if Adventurer is already "on" and needs to be added
		elif ( get_switch_flag( attachee ) ):
			game.party[0].follower_add( attachee )
			set_switch_flag( attachee, 0 )
	# Check if script is being run by Shopkeeper
	elif Shopkeeper_Is( attachee ):
		# Gets handle on Adventurer
		Adventurer = Find_NPC( attachee, 'Adventurer' )
		# Check if Adventurer is "on" and is not in party and is in the same spot as Shopkeeper
		if ( Adventurer != OBJ_HANDLE_NULL and Adventurer.leader_get() == OBJ_HANDLE_NULL and location_to_axis( Adventurer.location ) == location_to_axis( attachee.location ) ):
			# Faces Adventurer in the same direction as Shopkeeper and performs "on.off" switch on both
			Adventurer.rotation = attachee.rotation
			attachee.object_flag_unset(OF_OFF)
			Adventurer.object_flag_set(OF_OFF)
	return attachee

def Shopkeeper_Is( npc ): # Returns 1 if npc is Shopkeeper and 0 if it is not.
	return ( npc.obj_get_int( obj_f_critter_description_unknown ) == 20000 )

def Find_NPC( obj , querry): # Finds the npc requested by querry
	for npc in game.obj_list_vicinity( obj.location, OLC_NPC ):
		if ( npc.name == 8054 ):
			if ( querry == 'Shopkeeper' and Shopkeeper_Is( npc ) ):
				return npc
			elif (querry == 'Adventurer' and not Shopkeeper_Is( npc ) ):
				return npc
	return OBJ_HANDLE_NULL
		

def hireling_add(npc, pc): # Created for Shiningted for KotB.  Not used here, but preserved here for posterity.
	pc.ai_follower_add(npc)
	game.timevent_add(pc.follower_remove, (npc), 259200000)

def respawn(attachee):
	box = find_container_near(attachee,1001)
	RespawnInventory(box)
	game.timevent_add(respawn, (attachee), 86400000 ) #86400000ms is 24 hours
	return

##################################################################################################
